package com.jy.modules.cims.quota.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.controller.ExportController.ExportUtil;
import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.common.sysUtils.DateTimeUtils;
import com.jy.modules.cims.common.sysUtils.SimpleStringUtils;
import com.jy.modules.cims.component.sysAssist.ExportAndImport;
import com.jy.modules.cims.data.dto.QuotaEstimateDto;
import com.jy.modules.cims.quota.service.QuotaEstimateService;
import com.jy.platform.core.message.DataMsg;
import com.jy.platform.core.message.PageParameter;
import com.jy.platform.core.message.PageUtil;
import com.jy.platform.core.message.QueryReqBean;
import com.jy.platform.restservice.exception.AbaboonException;
import com.jy.platform.restservice.web.base.BaseController;
/**
 * 自动匹配债权预留额度
 * ClassName: QuotaEstimateController 
 * @Description: TODO
 * @author dell
 * @date 2015-11-5
 */
@Controller
@Scope("prototype")
@RequestMapping("/quotaEstimateController")
public class QuotaEstimateController extends BaseController {

	
	@Autowired
	@Qualifier("com.jy.modules.cims.quota.service.QuotaEstimateService")
	public QuotaEstimateService quotaEstimateService;
	
	@RequestMapping(value = "/prepareExecute/{operate}")
    public ModelAndView execute(@PathVariable("operate") String operate) throws AbaboonException {
        ModelAndView model = new ModelAndView();
        if("toQuery".equals(operate)){//跳转至 查询页面
            model.setViewName("cims/quotaDetail/queryQuotaEstimate");
            return model;
        }
        return model;
	}
	
	@RequestMapping(value = "/queryQuotaEstimateList")
	@ResponseBody
	public DataMsg queryQuotaEstimateList(HttpServletRequest request,HttpServletResponse response,QuotaEstimateDto dto, @ModelAttribute DataMsg dataMsg)throws Exception{
		response.reset();
		try {
			
		
		Map<String, Object> searchParams = new HashMap<String, Object>();
        QueryReqBean params = new QueryReqBean();
        params.setSearchParams(searchParams);
    	PageParameter pageInfo = PageUtil.toPageParameter(dataMsg);
		params.setPageParameter(pageInfo);
		if(SimpleStringUtils.hasText(dto.getStartDate())){//如果有账单日才进行查询
			

			searchParams.put("dto", dto);
			List<QuotaEstimateDto> list = quotaEstimateService.searchQuotaEstimateDtoByPaging(params.getSearchParams());
		    dataMsg.setData(list);
		}else{
			dataMsg.setData(null);
		}
		
        dataMsg.setTotalRows(pageInfo.getTotalCount());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dataMsg;
	}
	/**
	 * 
	 * @Description  计算总是请求
	 * @author zhangmi
	 * @date 2015-11-23
	 */
	@RequestMapping(value = "/getQuotaEstimateSum")
	@ResponseBody
	public DataMsg getQuotaEstimateSum(HttpServletRequest request,QuotaEstimateDto dto, @ModelAttribute DataMsg dataMsg)throws Exception{
		try {
		Map<String, Object> searchParams = new HashMap<String, Object>();
        QueryReqBean params = new QueryReqBean();
        params.setSearchParams(searchParams);
    	PageParameter pageInfo = PageUtil.toPageParameter(dataMsg);
		params.setPageParameter(pageInfo);
		if(SimpleStringUtils.hasText(dto.getStartDate())){//如果有账单日才进行查询
			searchParams.put("dto", dto);
			List<QuotaEstimateDto> listSum = quotaEstimateService.getQuotaEstimateDtoSum(params.getSearchParams());
			if(CollectionsUtils.isNotEmpty(listSum)){
			    dataMsg.setData(listSum);
			}else{
				dataMsg.setData(listSum);
			}
		}else{
			dataMsg.setData(null);
		}
        dataMsg.setTotalRows(pageInfo.getTotalCount());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dataMsg;
	}
	/**
	 * 
	 * @Description 导出--自动匹配债权预留债权额度试算
	 * ModelAndView  
	 * @author zhangmi
	 * @date 2015-11-5
	 */
	@RequestMapping(value = "/exportQuotaEstimateList")
    public ModelAndView exportQuotaEstimateList(HttpServletRequest request, QuotaEstimateDto dto, HttpServletResponse response,@ModelAttribute DataMsg dataMsg){
    	try {
	    	Map<String, Object> searchParams = new HashMap<String, Object>();
	    	searchParams.put("dto", dto);
	        QueryReqBean params = new QueryReqBean();
	        params.setSearchParams(searchParams);
	        List<QuotaEstimateDto> list = new ArrayList<QuotaEstimateDto>();
	        if(SimpleStringUtils.hasText(dto.getStartDate())){//如果有账单日才进行查询
	        	list = quotaEstimateService.exportQuotaEstimateDtoList(params.getSearchParams());
	        }
	    	ExportAndImport<QuotaEstimateDto> eai = new ExportAndImport<QuotaEstimateDto>();
	        String fileName = "自动匹配预留债权额度试算"+DateTimeUtils.getDateTime("yyyyMMddHHmmss");
	        String quotaEstimateCode = CimsConstant.QUOTA_ESTIMATE_CODE;
	        String quotaEstimateName = CimsConstant.QUOTA_ESTIMATE_NAME;
	        HSSFWorkbook workbook = eai.exportExcelSubsidiary(fileName, quotaEstimateCode, quotaEstimateName, list);
	        ExportUtil.write(workbook, fileName, response);
	    	return null;
    	} catch (Exception e) {
    		e.printStackTrace();
            logger.error("执行方法exportQuotaEstimateList异常-导出自动匹配预留债权试算:", e);
            ModelAndView m = new ModelAndView();
            m.setViewName("cims/quotaDetail/queryQuotaEstimate");
            m.addObject("error", "导出报表异常！");
            return m;
		}
    }
}
